<?php

namespace App\Http\Controllers;

use App\Http\Models\AlipayWithdraw;
use App\Http\Models\Feedback;
use App\Http\Models\Grade;
use App\Http\Models\Notice;
use App\Http\Models\ObjectModel;
use App\Http\Models\Order;
use App\Http\Models\PayRequest;
use App\Http\Models\Record;
use App\Http\Models\Remind;
use App\Http\Models\System;
use App\Http\Models\User;
use App\Http\Models\WithdrawRequest;
use EasyWeChat\Foundation\Application;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class ApplicationController extends Controller
{
    public $tel;
    public $code;

    public function __construct()
    {
        $this->tel  = session('tel');
        $this->code = session('code');
    }

    public function home()
    {
        $user = User::where('id_wechat', $this->tel)->first();
        if (!$user) {
            return redirect('/user-login');
        }

        return redirect('/objects');
    }

    public function objects()
    {
        $user   = User::where('id_wechat', $this->tel)->first();
        $notice = Notice::first();
        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }
        $objects = ObjectModel::where('is_disabled', '0')->orderBy('body_rank', 'desc')->get();

        return view('apps.objects', [
            'navigator'  => 'objects',
            'controller' => 'objectsController',
            'user'       => $user,
            'notice'     => $notice,
            'objects'    => $objects,
        ]);
    }

    public function objectsDetail($id, $period)
    {
        $user = User::where('id_wechat', $this->tel)->first();
        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }
        $object = ObjectModel::find($id);

        return view('apps.objectsDetail', [
            'navigator'  => 'objects',
            'controller' => 'objectsDetailController',
            'user'       => $user,
            'item'       => $object,
            'period'     => $period,
        ]);
    }

    public function ordersHold()
    {
        $user = session('wechat.oauth_user');
        $user = User::where('id_wechat', $this->tel)->first();
        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }
        $orders = Order::orderBy('created_at', 'desc')->where('id_user', $this->tel)->where('striked_at', '0000-00-00 00:00:00')->get();

        return view('apps.ordersHold', [
            'navigator'  => 'ordersHold',
            'controller' => 'ordersHoldController',
            'user'       => $user,
            'orders'     => $orders,
        ]);
    }

    public function ordersHistory()
    {
        $user = User::where('id_wechat', $this->tel)->first();
        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }
        $orders = Order::orderBy('created_at', 'desc')->where('id_user', $this->tel)->where('striked_at', '<>', '0000-00-00 00:00:00')->paginate(20);

        return view('apps.ordersHistory', [
            'navigator'  => 'ordersHistory',
            'controller' => 'ordersHistoryController',
            'user'       => $user,
            'orders'     => $orders,
        ]);
    }

    public function ordersDetail($id)
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        $item = Order::find($id);

        return view('apps.ordersDetail', [
            'navigator' => 'orders',
            'user'      => $user,
            'item'      => $item,
        ]);
    }

    public function account()
    {
        $user = User::where('id_wechat', $this->tel)->first();
        switch ($user->level_id) {
            case 0:
                $user->level = '游客';
                break;
            case 1:
                $user->level = '大众会员';
                break;
            case 2:
                $user->level = '黄金会员';
                break;
            case 3:
                $user->level = '铂金会员';
                break;
            case 4:
                $user->level = '钻石会员';
                break;
        }

        $notice = Notice::first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        $count_refers = User::where('id_introducer', $this->tel)->count();

        $count_bonus = 0;
        $records     = Record::where('id_user', $this->tel)->where('id_refer', '>', 0)->get();
        foreach ($records as $record) {
            $count_bonus = $count_bonus + $record->body_stake;
        }

        return view('application.account', [
            'title'        => '会员中心',
            'user'         => $user,
            'count_refers' => $count_refers,
            'notice'       => $notice->notice,
            'count_bonus'  => $count_bonus,
        ]);
    }

    public function team(Request $request)
    {
        $user      = User::where('id_wechat', $this->tel)->first();
        $recommend = User::where('id_wechat', $user->id_introducer)->first();
        if ($recommend) {
            switch ($recommend->level_id) {
                case 0:
                    $recommend->level = '游客';
                    break;
                case 1:
                    $recommend->level = '大众会员';
                    break;
                case 2:
                    $recommend->level = '黄金会员';
                    break;
                case 3:
                    $recommend->level = '铂金会员';
                    break;
                case 4:
                    $recommend->level = '钻石会员';
                    break;
            }
        }

        return view('application.accountTeam', [
            'title'     => '我的团队',
            'recommend' => $recommend,
        ]);
    }

    public function accountBind(Request $request)
    {
        if ($request->isMethod('post')) {
            $input = Input::all();
            // if ($input['vcode'] != Cache::get('verify_code' . $input['mobile'])) {
            //     return view('application.info', [
            //         'title' => '绑定失败',
            //         'icon' => 'warn',
            //         'content' => '您填写的验证码不正确'
            //     ]);
            // }
            if (empty($input['tid'])) {
                return view('application.info', [
                    'title'   => '绑定失败',
                    'icon'    => 'warn',
                    'content' => '推荐码必填',
                ]);
            }
            // $tjr = User::where('tjm', $input['tid'])->where('grade', 1)->first();
            $tjr = User::where('tjm', $input['tid'])->where('is_disabled', 0)->first();
            if (empty($tjr)) {
                echo '<script type="text/javascript">';
                echo 'alert("推荐码不存在，请确认推荐码");';
                echo 'window.history.back();';
                echo '</script>';
            } else {
                $user = User::where('id_wechat', $input['mobile'])->first();
                if (empty($user)) {
                    $user                = new User();
                    $user->id_wechat     = $input['mobile'];
                    $user->body_phone    = $input['mobile'];
                    $user->password      = md5($input['password']);
                    $user->tjm           = strtolower(str_random(6));
                    $user->id_introducer = $tjr->id_wechat;
                    $user->grade         = 1;  //注册就是代理
                    // $user->body_balance = '2000';
                    $user->save();
                    //升级
                    $count = User::where('id_introducer', $tjr->id_wechat)->count();
                    if ($count >= 3) {  //黄金会员
                        $tjr->level_id = 2;
                    }
                    if ($count >= 5) {  //白金会员
                        $tjr->level_id = 3;
                    }
                    if ($count >= 10) {  //钻石会员
                        $tjr->level_id = 4;
                    }
                    $tjr->save();
                    $remind           = new Remind();
                    $remind->type     = 1;
                    $remind->tel      = $user->id_wechat;
                    $remind->add_time = time();
                    $remind->save();
                    //$payRequest = new PayRequest;
                    // $payRequest->id_user =  $user->id_wechat;
                    // $payRequest->body_transfer_number = 'TEST';
                    //	$payRequest->body_stake = '2000';
                    // $payRequest->body_gateway = 'online';
                    // $payRequest->processed_at =  date('Y-m-d H:i:s', time());
                    // $payRequest->save();

                    // $record = new Record;
                    // $record->id_user = $user->id_wechat;
                    // $record->id_payRequest = $payRequest->id;
                    //  $record->body_name = '平台测试';
                    //  $record->body_direction = 1;
                    // $record->body_stake = '2000';
                    // $record->save();

                    // 注册chatim项目的会员
                    $data['id']          = $user->id;
                    $data['number']      = $user->id;
                    $data['name']        = $input['mobile'];
                    $data['nickname']    = $input['mobile'];
                    $data['pwd']         = md5($input['password']);
                    $data['avatar']      = 'static/avatar/20190612152601.jpg';
                    // $data['accessToken'] = $this->set_randcode(32);
                    // $data['deviceInfo']  = '';
                    Config::set('database.connections.mysql.host', env('DB_HOST'));
                    Config::set('database.connections.mysql.database', 'chatim');
                    Config::set('database.connections.mysql.username', env('DB_USERNAME'));
                    Config::set('database.connections.mysql.password', env('DB_PASSWORD'));
                    DB::reconnect(); //重连数据库
                    DB::table('app_user')->insert($data);

                    session(['tel' => $input['mobile']]);

                    return redirect('/');
                }

                return view('application.info', [
                            'title'   => '注册失败',
                            'icon'    => 'warn',
                            'content' => '该手机号已存在',
                        ]);
            }
        } else {
            //$id_introducer = User::whereId($request->get('tid'))->first();
            $id_introducer = User::where('tjm', $request->get('tid'))->where('grade', 1)->first();
            if (empty($id_introducer)) {
                return view('application.accountBind', [
                    'title' => '注册',
                    'tid'   => '',
                ]);
            }

            return view('application.accountBind', [
                    'title' => '注册',
                    'tid'   => $id_introducer->tjm,
                ]);
        }
    }

    public function set_randcode($num)
    {
        $str  = 'qwertyuiopasdfghjklzxcvbnm1234567890';
        $temp = '';
        for ($i = 0; $i < $num; ++$i) {
            $rand = rand(0, 35);
            $temp .= substr($str, $rand, 1);
        }

        return $temp;
    }

    /**
     * 用户充值中心.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function accountPay(Request $request)
    {
        $user = User::where('id_wechat', $this->tel)->first();
        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }
        $system = System::findOrFail(1);
        $tel    = session('tel');

        return view('application.accountPay', [
            'title'  => '我要充值',
            'system' => $system,
            'tel'    => $tel,
        ]);
    }

    public function accountPayStaff(Application $wechat)
    {
        return view('application.accountPayStaff', [
            'title' => '人工充值',
        ]);
    }

    /**
     * 提现记录.
     *
     * @param Application $wechat
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function accountWithdrawRecords()
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        $withdrawRequests = WithdrawRequest::where('id_user', $this->tel)->orderBy('created_at', 'desc')->get();

        return view('application.accountWithdrawRecords', [
            'title'            => '提现记录',
            'withdrawRequests' => $withdrawRequests,
        ]);
    }

    public function Wechatpay($price)
    {
        require_once '../wxpay/WxPay.Config.php';
        require_once '../wxpay/WxPay.Data.php';
        require_once '../wxpay/WxPay.NativePay.php';
        require_once '../wxpay/WxPay.Api.php';

        $tijiao = 'http://' . $_SERVER['SERVER_NAME'] . '/wxpay/notify.php';
        $notify = new \NativePay();

        $money = $price * 6.8 * 100; //自己刚才输入的金额一

        $user = User::where('id_wechat', $this->tel)->first();

        $input = new \WxPayUnifiedOrder();
        $input->SetBody(env('APP_TITLE'));
        $input->SetAttach($user->id_wechat);
        $input->SetOut_trade_no((date('YmdHis')));
        $input->SetTotal_fee($money);
        $input->SetTime_start(date('YmdHis'));
        $input->SetTime_expire(date('YmdHis', time() + 600));
        $input->SetGoods_tag(env('APP_TITLE'));
        $input->SetNotify_url($tijiao);
        $input->SetTrade_type('NATIVE');
        $input->SetProduct_id('123456789');
        $result = $notify->GetPayUrl($input);

        if ('SUCCESS' == $result['return_code']) {
            //这个if里面的就是自己定义的内容，比如生成二维码之后你要插入一条数据进入数据库之类  上面组装是必须要有的 下面这个是自己自定义的

            $url = $result['code_url'];

            return view('application.WxPayDir', ['url' => $url]);
        }

        return view('application.WxPayerror', ['result' => $result]);
        //微信支付结尾
    }

    public function level()
    {
        return view('application.level', [
            'title' => '等级说明',
        ]);
    }

    public function ring()
    {
        return view('application.ring', [
            'title' => '发圈奖励',
        ]);
    }

    public function wxgzh($price)
    {
        ini_set('date.timezone', 'Asia/Shanghai');

        require_once '../wxpay/log.php';
        require_once '../wxpay/WxPay.JsApiPay.php';
        require_once '../wxpay/WxPay.Api.php';
        $tijiao = 'http://' . $_SERVER['SERVER_NAME'] . '/wxpay/notify.php';
        $user   = User::where('id_wechat', $this->tel)->first();

        $money = $price * 6.8 * 100; //自己刚才输入的金额一
        //初始化日志
        $logHandler = new \CLogFileHandler('../wxpay/logs/' . date('Y-m-d') . '.log');
        $log        = \Log::Init($logHandler, 15);

        //打印输出数组信息
        function printf_info($data)
        {
            foreach ($data as $key => $value) {
                echo "<font color='#00ff55;'>$key</font> : $value <br/>";
            }
        }

        //①、获取用户openid
        $tools  = new \JsApiPay();
        $openId = $tools->GetOpenid();

        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody(env('APP_TITLE'));
        $input->SetAttach($user->id_wechat);
        $input->SetOut_trade_no(date('YmdHis'));
        $input->SetTotal_fee($money);
        $input->SetTime_start(date('YmdHis'));
        $input->SetTime_expire(date('YmdHis', time() + 600));
        $input->SetGoods_tag(env('APP_TITLE'));
        $input->SetNotify_url($tijiao);
        $input->SetTrade_type('JSAPI');
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
        //printf_info($order);
        $jsApiParameters = $tools->GetJsApiParameters($order);

        //获取共享收货地址js函数参数
        $editAddress = $tools->GetEditAddressParameters();

        //③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
        /*
         * 注意：
         * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
         * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
         * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
         */
        return view('application.accountPaygzh', [
            'order'           => $order,
            'jsApiParameters' => $jsApiParameters,
            'editAddress'     => $editAddress,
        ]);
    }

    public function zypay($price)
    {
        $user                     = User::where('id_wechat', $this->tel)->first();
        $payRequest               = new PayRequest();
        $payRequest->id_user      = $user->id_wechat;
        $price                    = $price * 6.8;
        $payRequest->body_stake   = $price;
        $payRequest->body_gateway = 'online';
        $payRequest->save();

        $parameterForRequest = '';
        $parameterForSign    = '';
        $parameters          = [
            'pay_memberid'    => env('PAYMENT_PID'),
            'pay_orderid'     => date('YmdHis'),
            'pay_amount'      => $payRequest->body_stake,
            'pay_applydate'   => date('Y-m-d H:i:s'),
            'pay_bankcode'    => 'WXZF',
            'pay_notifyurl'   => env('PAYMENT_URL_NO'),
            'pay_callbackurl' => env('PAYMENT_URL_RE'),
        ];
        ksort($parameters);
        reset($parameters);
        foreach ($parameters as $key => $value) {
            $parameterForSign = $parameterForSign . $key . '=>' . $value . '&';
        }
        $sign                      = strtoupper(md5($parameterForSign . 'key=' . env('PAYMENT_KEY')));
        $parameters['pay_md5sign'] = $sign;

        $pay_reserved1 = $payRequest->id; /*新增*/
        $tongdao       = env('PAYMENT_WXTD');
        $requestURL    = 'http://zf.cnzypay.com/Pay_Index.html';

        return view('application.accountPayRedirect', [
            'pay_reserved1' => $pay_reserved1,
            'tongdao'       => $tongdao,
            'requestURL'    => $requestURL,
            'parameters'    => $parameters,
            'sign'          => $sign,
        ]);
    }

    public function zypayb($price)
    {
        $user                     = User::where('id_wechat', $this->tel)->first();
        $payRequest               = new PayRequest();
        $payRequest->id_user      = $user->id_wechat;
        $payRequest->body_stake   = $price * 6.8;
        $payRequest->body_gateway = 'online';
        $payRequest->save();

        $parameterForRequest = '';
        $parameterForSign    = '';
        $parameters          = [
            'pay_memberid'    => env('PAYMENT_PID'),
            'pay_orderid'     => date('YmdHis'),
            'pay_amount'      => $payRequest->body_stake,
            'pay_applydate'   => date('Y-m-d H:i:s'),
            'pay_bankcode'    => 'alipay',
            'pay_notifyurl'   => env('PAYMENT_URL_NO'),
            'pay_callbackurl' => env('PAYMENT_URL_RE'),
        ];
        ksort($parameters);
        reset($parameters);
        foreach ($parameters as $key => $value) {
            $parameterForSign = $parameterForSign . $key . '=>' . $value . '&';
        }
        $sign                      = strtoupper(md5($parameterForSign . 'key=' . env('PAYMENT_KEY')));
        $parameters['pay_md5sign'] = $sign;

        $pay_reserved1 = $payRequest->id; /*新增*/
        $tongdao       = env('PAYMENT_ALITD');
        $requestURL    = 'http://zf.cnzypay.com/Pay_Index.html';

        return view('application.accountPayRedirect', [
            'pay_reserved1' => $pay_reserved1,
            'tongdao'       => $tongdao,
            'requestURL'    => $requestURL,
            'parameters'    => $parameters,
            'sign'          => $sign,
        ]);
    }

    /**
     * 申请代理.
     */
    public function agentsj(Request $request)
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ('1' == $user->grade) {
            return view('application.info', [
                'title'   => '申请已经通过',
                'icon'    => 'success',
                'content' => '恭喜你，你已经成为我们的代理<br>后台所需账号密码和前台一致。<br>请用电脑登录代理后台 地址<br><br>' . 'http://' . $_SERVER['SERVER_NAME'] . '/agent',
            ]);
        }

        if ('2' == $user->grade) {
            return view('application.info', [
                'title'   => '申请正在审核中',
                'icon'    => 'warn',
                'content' => '您的申请已经提交，中途无法修改信息。',
            ]);
        }

        if ('3' == $user->grade) {
            return view('application.info', [
                'title'   => '禁止申请',
                'icon'    => 'warn',
                'content' => '您的账号因相关原因，被永远禁止申请代理。',
            ]);
        }

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        if ($request->isMethod('post')) {
            if (
                !$request->input('name', null)
                || !$request->input('number', null)
            ) {
                return view('application.info', [
                    'title'   => '申请失败',
                    'icon'    => 'warn',
                    'content' => '请将表单填写完整，谢谢',
                ]);
            }

            $sjs          = (mt_rand(10, 99));
            $user->grade  = '2';
            $user->rname  = $request->input('name');
            $user->idcard = $request->input('number');
            $user->tjm    = '8' . $sjs . $user->id;
            $user->save();

            return view('application.info', [
                'title'   => '申请成功',
                'icon'    => 'success',
                'content' => '理我们已经收到您的代理申请，将在24小时内处理',
            ]);
        }

        return view('application.agentsj', [
                'title' => '我要申请代理',
                'user'  => $user,
            ]);
    }

    public function onlineChat()
    {
        $user = User::where('id_wechat', $this->tel)->first();

        return view('apps.onlineChat', [
            'active' => 'onlineChat',
            'user'   => $user,
        ]);
    }

    /**
     * 提现页面.
     *
     * @param Application $wechat
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function accountWithdraw(Request $request)
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        if ($request->isMethod('post')) {
            if (
                !$request->input('name', null)
                || !$request->input('number', null)
                || !$request->input('bank', null)
                || !$request->input('deposit', null)
                || !$request->input('stake', null)
            ) {
                return view('application.info', [
                    'title'   => '提现失败',
                    'icon'    => 'warn',
                    'content' => '请将表单填写完整，谢谢',
                ]);
            }

            if ($request->input('codes') != $this->code) {
                return view('application.info', [
                    'title'   => '绑定失败',
                    'icon'    => 'warn',
                    'content' => '您填写的验证码不正确',
                ]);
            }
            //后台限定金额
            // $system = System::findOrFail(1);
            // $m = $system->withdraw;
            // if (intval($request->input('stake', 0)) < $m) {
            //     return view('application.info', [
            //         'title' => '提现失败',
            //         'icon' => 'warn',
            //         'content' => "單次提现金額不得低於 $m 元"
            //     ]);
            // }
            $price      = intval($request->input('stake', 0));
            $handleFee  = $price * 0.02;
            $priceTotal = $price + $handleFee;
            if ($priceTotal > intval($user->body_balance)) {
                return view('application.info', [
                    'title'   => '提现失败',
                    'icon'    => 'warn',
                    'content' => '你当前余额不足',
                ]);
            }

            // $orderSum = Order::where('id_user', $this->tel)->sum('body_stake');
            // if (intval($orderSum) < 300) {
            //     return view('application.info', [
            //         'title' => '提现失败',
            //         'icon' => 'warn',
            //         'content' => '为避免恶意透支，累积交易金额超过 300 元即可提现'
            //     ]);
            // }

            DB::beginTransaction();

            $user->body_balance = $user->body_balance - $priceTotal;
            $user->save();
            $sr_stake = $request->input('stake');
            if ($user->body_balance < 0) {
                DB::rollback();
            } else {
                $withdrawRequest                  = new WithdrawRequest();
                $withdrawRequest->id_user         = $this->tel;
                $withdrawRequest->body_stake      = $sr_stake;
                $withdrawRequest->body_name       = $request->input('name');
                $withdrawRequest->body_bank       = $request->input('bank');
                $withdrawRequest->body_deposit    = $request->input('deposit');
                $withdrawRequest->body_number     = $request->input('number');
                $withdrawRequest->body_handle_fee = $withdrawRequest->body_stake * 0.02;
                $withdrawRequest->save();

                $record                     = new Record();
                $record->id_user            = $this->tel;
                $record->id_withdrawRequest = $withdrawRequest->id;
                $record->body_name          = '结余提现';
                $record->body_direction     = 0;
                $record->body_stake         = $withdrawRequest->body_stake;
                $record->save();

                $remind           = new Remind();
                $remind->type     = 2;
                $remind->tel      = $user->id_wechat;
                $remind->add_time = time();
                $remind->save();
            }

            DB::commit();

            return view('application.info', [
                'title'   => '申请成功',
                'icon'    => 'success',
                'content' => '我们已经收到您的提现申请，将在24小时内处理',
            ]);
        }

        return view('application.accountWithdraw', [
                'title' => '我要提现',
                'user'  => $user,
            ]);
    }

    public function accountAlipayWithdraw(Request $request)
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        if ($request->isMethod('post')) {
            if (
                !$request->input('name', null)
                || !$request->input('username', null)
                || !$request->input('price', null)
            ) {
                return view('application.info', [
                    'title'   => '提现失败',
                    'icon'    => 'warn',
                    'content' => '请将表单填写完整，谢谢',
                ]);
            }

            $price      = intval($request->input('price', 0));
            $handleFee  = $price * 0.02;
            $priceTotal = $price + $handleFee;
            if ($priceTotal > intval($user->body_balance)) {
                return view('application.info', [
                    'title'   => '提现失败',
                    'icon'    => 'warn',
                    'content' => '你当前余额不足',
                ]);
            }
            // $orderSum = Order::where('id_user', $this->tel)->sum('body_stake');
            // if (intval($orderSum) < 300) {
            //     return view('application.info', [
            //         'title' => '提现失败',
            //         'icon' => 'warn',
            //         'content' => '为避免恶意透支，累积交易金额超过 300 元即可提现'
            //     ]);
            // }

            DB::beginTransaction();
            $user->body_balance = $user->body_balance - $priceTotal;
            $user->save();
            if ($user->body_balance < 0) {
                DB::rollback();
            } else {
                $withdrawRequest             = new AlipayWithdraw();
                $withdrawRequest->id_user    = $this->tel;
                $withdrawRequest->name       = $request->input('name');
                $withdrawRequest->username   = $request->input('username');
                $withdrawRequest->price      = $request->input('price');
                $withdrawRequest->handle_fee = $withdrawRequest->price * 0.02;
                // $withdrawRequest->created_at = date('Y-m-d H:i:s', time());
                $withdrawRequest->save();

                $record                     = new Record();
                $record->id_user            = $this->tel;
                $record->id_withdrawRequest = $withdrawRequest->id;
                $record->body_name          = '结余提现（支付宝）';
                $record->body_direction     = 0;
                $record->body_stake         = $withdrawRequest->price;
                $record->save();

                $remind           = new Remind();
                $remind->type     = 2;
                $remind->tel      = $user->id_wechat;
                $remind->add_time = time();
                $remind->save();
            }

            DB::commit();

            return view('application.info', [
                'title'   => '申请成功',
                'icon'    => 'success',
                'content' => '我们已经收到您的提现申请，将在24小时内处理',
            ]);
        }

        return view('application.accountAlipayWithdraw', [
                'title' => '我要提现',
                'user'  => $user,
            ]);
    }

    public function accountRecords()
    {
        $user = session('wechat.oauth_user');
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        $records = Record::orderBy('created_at', 'desc')->where('id_user', $this->tel)->paginate(20);

        return view('application.accountRecords', [
            'title'   => '资金记录',
            'records' => $records,
        ]);
    }

    public function accountMoney(Request $request)
    {
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        if ($request->isMethod('post')) {
            if (!$request->input('price', null)) {
                return view('application.info', [
                    'title'   => '充值失败',
                    'icon'    => 'warn',
                    'content' => '请将表单填写完整，谢谢',
                ]);
            }

            $price = intval($request->input('price', 0));
            if ($price > intval($user->body_balance)) {
                return view('application.info', [
                    'title'   => '充值失败',
                    'icon'    => 'warn',
                    'content' => '你当前余额不足',
                ]);
            }
            // $orderSum = Order::where('id_user', $this->tel)->sum('body_stake');
            // if (intval($orderSum) < 300) {
            //     return view('application.info', [
            //         'title' => '提现失败',
            //         'icon' => 'warn',
            //         'content' => '为避免恶意透支，累积交易金额超过 300 元即可提现'
            //     ]);
            // }

            DB::beginTransaction();
            $user->body_balance = $user->body_balance - $price;
            $user->money        = $user->money + $price;
            $user->save();
            if ($user->body_balance < 0) {
                DB::rollback();
            } else {
                $record                 = new Record();
                $record->id_user        = $this->tel;
                $record->body_name      = '结余提现（支付宝）';
                $record->body_direction = 0;
                $record->body_stake     = $price;
                $record->save();

                $remind           = new Remind();
                $remind->type     = 2;
                $remind->tel      = $user->id_wechat;
                $remind->add_time = time();
                $remind->save();
            }

            DB::commit();

            return view('application.info', [
                'title'   => '充值成功',
                'icon'    => 'success',
                'content' => '您已充值成功本金锁仓！',
            ]);
        }

        return view('application.accountMoney', [
                'title' => '充值本金锁仓',
                'user'  => $user,
            ]);
    }

    public function accountOrders()
    {
        $user = session('wechat.oauth_user');
        $user = User::where('id_wechat', $this->tel)->first();

        if ($user->is_disabled > 0) {
            return $this->denialUser();
        }

        $orders = Order::orderBy('created_at', 'desc')->where('id_user', $this->tel)->paginate(20);

        return view('application.accountOrders', [
            'title'  => '交易记录',
            'orders' => $orders,
        ]);
    }

    public function accountExpand($id)
    {
        $user = User::where('id_wechat', $this->tel)->where('grade', 1)->first();
        if (empty($user)) {
            return view('application.info', [
                'title'   => '没有推广链接',
                'icon'    => 'warn',
                'content' => '您还不是我们的代理商，没有您的推广链接',
            ]);
        }
        $url = 'http://gzmxkj.com/account/bind?tid=' . $user->tjm;

        return view('application.accountExpand', [
                'qrcode' => \SimpleSoftwareIO\QrCode\Facades\QrCode::size(200)->generate($url),
                'url'    => $url,
                'tid'    => $user->tjm,
            ]);
    }

    public function appdown(Application $wechat)
    {
        $url = 'http://' . $_SERVER['SERVER_NAME'] . '/app.apk';

        return view('application.appdown', [
            'qrcode' => \SimpleSoftwareIO\QrCode\Facades\QrCode::size(200)->generate($url),
            'url'    => $url,
        ]);
    }

    public function xftali(Application $wechat)
    {
        $url = 'http://' . $_SERVER['SERVER_NAME'] . '/app.apk';

        return view('application.xftali', [
            'qrcode' => \SimpleSoftwareIO\QrCode\Facades\QrCode::size(200)->generate($url),
            'url'    => $url,
        ]);
    }

    /**
     * 验证码生成.
     *
     * @param [type] $tmp [description]
     *
     * @return [type] [description]
     */
    public function captcha($tmp)
    {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder();
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header('Cache-Control: no-cache, must-revalidate');
        header('Content-Type: image/jpeg');
        $builder->output();
    }

    public function support(Application $wechat)
    {
        return view('application.support', [
            'title' => '在线咨询',
        ]);
    }

    public function supportFaq(Application $wechat)
    {
        return view('application.supportFaq', [
            'title' => '常见问题',
        ]);
    }

    public function supportService(Application $wechat)
    {
        return view('application.supportService', [
            'title' => '在线客服',
        ]);
    }

    public function supportFeedback(Request $request, Application $wechat)
    {
        if ($request->isMethod('post')) {
            if (
                !$request->input('content', null)
                || !$request->input('tool', null)
                || !$request->input('number', null)
            ) {
                return view('application.info', [
                    'title'   => '反馈失败',
                    'icon'    => 'warn',
                    'content' => '请將表單填寫完整，謝謝',
                ]);
            }

            $feedback               = new Feedback();
            $feedback->body_content = $request->input('content');
            $feedback->body_tool    = $request->input('tool');
            $feedback->body_number  = $request->input('number');
            $feedback->save();

            return view('application.info', [
                'title'   => '反馈成功',
                'icon'    => 'success',
                'content' => '我们已经收到您的反馈，谢谢',
            ]);
        }

        return view('application.supportFeedback', [
                'title' => '意见反馈',
            ]);
    }

    /**
     * 获取短信发送
     *
     * @return string
     */
    public function checkC()
    {
        $input = Input::all();
        $code  = $input['code'];
        if (Session::get('milkcaptcha') != $code) {
            die(json_encode('图形验证码错误'));
        }
        die(json_encode(1));
    }

    public function postSMS()
    {
        session(['code' => '']);
        $code  = rand(1000, 9999);
        $input = Input::all();
        $tel   = $input['tel'];
        header('content-type:text/html;charset=utf-8');
        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $smsConf = [
            'key'       => 'fcc5a86dcd378f4b49b3c27247d3d5f7', //您申请的APPKEY
            'mobile'    => intval($tel), //接受短信的用户手机号码
            'tpl_id'    => '158236', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#=' . $code, //您设置的模板变量，根据实际情况修改
        ];

        $type = empty($input['type']) ? 0 : 1;
        if (1 == $type) {
            $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
            if ($content) {
                $result     = json_decode($content, true);
                $error_code = $result['error_code'];
                if (0 == $error_code) {
                    //状态为0，说明短信发送成功
                    session(['code' => $code]);

                    return 1;
                }
                //状态非0，说明失败
                $msg = $result['reason'];
                die(json_encode('短信发送失败' . $msg));
                //echo "短信发送失败(".$error_code.")：".$msg;
            }
            //返回内容异常，以下可根据业务逻辑自行修改
            die(json_encode('请求发送短信失败'));
        }
        if (User::where('id_wechat', $tel)->first()) {
            return '106';
        }
        $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信
        if ($content) {
            $result     = json_decode($content, true);
            $error_code = $result['error_code'];
            if (0 == $error_code) {
                //状态为0，说明短信发送成功
                session(['code' => $code]);

                return 1;
            }
            //状态非0，说明失败
            $msg = $result['reason'];
            die(json_encode('短信发送失败' . $msg));
            //echo "短信发送失败(".$error_code.")：".$msg;
        }
        //返回内容异常，以下可根据业务逻辑自行修改
        die(json_encode('请求发送短信失败'));
    }

    public function juhecurl($url, $params = false, $ispost = 0)
    {
        $httpInfo = [];
        $ch       = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '/' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if (false === $response) {
            //return "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);

        return $response;
    }

    public function postSMSs()
    {
        $input = Input::all();
        $input;
        $tel  = $input['tel'];
        $type = empty($input['type']) ? 0 : 1;
        if (1 == $type) {
            $code = rand(pow(10, (6 - 1)), pow(10, 6) - 1);
            session(['code' => $code]);
            $body   = urlencode('【云微赢】尊敬的用户，您的验证码是' . $code . '，请在10分钟内输入，客服微信13337710406。');
            $smsUrl = env('SMS_BASE') . env('SMS_KEY') . '&mobile=' . $tel . '&content=' . $body;
            $rel    = explode(',', file_get_contents($smsUrl));
            $rel    = explode(':', $rel[0]);
            $rel    = $rel[1];

            return $rel;
        }
        if (User::where('id_wechat', $tel)->first()) {
            return '106';
        }
        $code = rand(pow(10, (6 - 1)), pow(10, 6) - 1);
        session(['code' => $code]);
        $body   = urlencode('【云微赢】尊敬的用户，您的验证码是' . $code . '，请在10分钟内输入，客服微信13337710406。');
        $smsUrl = env('SMS_BASE') . env('SMS_KEY') . '&mobile=' . $tel . '&content=' . $body;
        $rel    = explode(',', file_get_contents($smsUrl));
        $rel    = explode(':', $rel[0]);
        $rel    = $rel[1];

        return $rel;
    }

    /**
     * 用户密码修改.
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function updatePassword(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            if (User::where('id', $id)->update(['password' => md5($request->get('password'))])) {
                echo '<script type="text/javascript">';
                echo 'alert("密码修改成功");';
                echo 'window.location.href="/account";';
                echo '</script>';
            } else {
                echo '<script type="text/javascript">';
                echo 'alert("密码修改失败");';
                echo 'window.location.href="/account";';
                echo '</script>';
            }
        } else {
            return view('application.accountPassword', [
                'title' => '修改密码',
            ]);
        }
    }

    /**
     * 用户登录.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        if ($request->isMethod('post')) {
            if (User::where('id_wechat', $request->get('id_wechat'))->where('password', md5($request->get('password')))->first()) {
                session(['tel' => $request->get('id_wechat')]);

                return redirect('/objects');
            }
            echo '<script type="text/javascript">';
            echo 'alert("手机号或者密码错误");';
            echo 'window.history.back();';
            echo '</script>';
        } else {
            return view('application.login', [
                'title' => '登录中心',
                'ver'   => '3.0.528',
            ]);
        }
    }

    /**
     * 密码找回手机验证
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function back(Request $request)
    {
        if ($request->isMethod('post')) {
            if ($request->get('vcode') != $this->code) {
                return view('application.info', [
                    'title'   => '验证失败',
                    'icon'    => 'warn',
                    'content' => '您填写的验证码不正确',
                ]);
            }
            //验证通过(验证手机号是否存在)
            if (User::where('id_wechat', $request->get('mobile'))->first()) {
                return redirect('/account/backPassword/' . $request->get('mobile'));
            }

            return view('application.info', [
                        'title'   => '请注册信息',
                        'icon'    => 'warn',
                        'content' => '无信息，请注册',
                    ]);
        }

        return view('application.accountBack', [
                'title' => '找回密码',
            ]);
    }

    /**
     * 密码找回.
     *
     * @param $tel
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function backPassword(Request $request, $tel)
    {
        if ($request->isMethod('post')) {
            if (User::where('id_wechat', $request->get('tel'))->update(['password' => md5($request->get('password'))])) {
                session(['tel' => $request->get('tel')]);
                echo '<script type = "text/javascript">';
                echo 'alert("密码修改成功");';
                if ($this->is_mobile()) {
                    echo 'window . location . href = "/objects";';
                } else {
                    echo 'window . location . href = "/account";';
                }
                echo '</script >';
            } else {
                echo '<script type = "text/javascript">';
                echo 'alert("密码修改失败");';
                echo 'window.history.back();';
                echo ' </script > ';
            }
        } else {
            return view('application.accountBackPassword', [
                'title' => '找回密码',
                'tel'   => $tel,
            ]);
        }
    }

    /**
     * 用户等级审核管理.
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function Grade(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->all();
            if (100 == $request->get('stake')) {
                $data['grade'] = 1;
            } elseif (200 == $request->get('stake')) {
                $data['grade'] = 2;
            } elseif (300 == $request->get('stake')) {
                $data['grade'] = 3;
            } elseif (400 == $request->get('stake')) {
                $data['grade'] = 4;
            }
            unset($data['stake']);
            $data['id_wechat'] = $this->tel;
            //判断该用户是否已经申请
            if ($info = Grade::where('id_wechat', $data['id_wechat'])->first()) {
                if ($data['grade'] == $info->grade) {
                    return view('application.info', [
                        'title'   => '等级申请',
                        'icon'    => 'warn',
                        'content' => '申请失败，不能选择自己拥有等级',
                    ]);
                }
                if ($data['grade'] < $info->grade) {
                    return view('application.info', [
                            'title'   => '等级申请',
                            'icon'    => 'warn',
                            'content' => '申请失败，不能低于当前等级!',
                        ]);
                }
                $data['old_grade'] = $info->grade;
                $data['status']    = 0;
                if (Grade::where('id', $info->id)->update($data)) {
                    echo '<script type = "text/javascript">';
                    echo 'alert("申请成功,请等待审核");';
                    echo 'window . location . href = "/account";';
                    echo '</script >';
                } else {
                    return view('application.info', [
                                'title'   => '等级申请',
                                'icon'    => 'warn',
                                'content' => '申请失败，请重试',
                            ]);
                }
            } else {
                if (Grade::create($data)) {
                    echo '<script type = "text/javascript">';
                    echo 'alert("申请成功,请等待审核");';
                    echo 'window . location . href = "/account";';
                    echo '</script >';
                } else {
                    return view('application.info', [
                        'title'   => '等级申请',
                        'icon'    => 'warn',
                        'content' => '申请失败，请重试',
                    ]);
                }
            }
        } else {
            $user = User::whereId($id)->first();

            return view('application.accountGrade', [
                'title' => '等级申请',
                'user'  => $user,
            ]);
        }
    }

    public function Extend($id)
    {
        $users = User::findOrFail($id);
        //我的上级
        if (!empty($users->id_introducer)) {
            $superior = $this->get_users($users->id_introducer);
        } else {
            $superior = null;
        }
        //我的下级列表
        $subordinate = User::where('id_introducer', $users->id_wechat)->get();
        //各等级的数量
        $data['one']   = User::where('id_introducer', $users->id_wechat)->where('grade', 1)->count();
        $data['two']   = User::where('id_introducer', $users->id_wechat)->where('grade', 2)->count();
        $data['three'] = User::where('id_introducer', $users->id_wechat)->where('grade', 3)->count();
        $data['four']  = User::where('id_introducer', $users->id_wechat)->where('grade', 4)->count();

        return view('application.accountExtend', [
            'title'       => '推广统计',
            'superior'    => $superior,
            'subordinate' => $subordinate,
            'data'        => $data,
        ]);
    }

    /**
     * 用户退出登录.
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function loginOut(Request $request)
    {
        $request->session()->flush();

        return redirect('/user-login');
    }

    //获取用户ID
    public function get_users($id)
    {
        $data = User::where('id_wechat', $id)->first();

        return $data->id_wechat;
    }

    public function recharge($dollar, $price)
    {
        header('Content-type:text/html; charset=utf-8');
        $huanxun = Config::get('huanxun');
        //$price = '0.01';
        $MerCode = $huanxun['PAY_MID']; //商户号
        $Account = $huanxun['ACCOUNT']; //账户号
        $Mer_key = $huanxun['PAY_KEY'];  //MD5
        //    $currUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://';
        //    $currUrl .= $_SERVER['HTTP_HOST'];
        $Version         = 'v1.0.0';
        $MerCode         = $MerCode;
        $MerName         = '';
        $Account         = $Account;
        $MsgId           = '';
        $ReqDate         = date('YmdHis');
        $Signature       = '';
        $MerBillNo       = $this->get_order_sn();  //商户订单号
        $GatewayType     = '01'; //支付方式
        $Date            = date('Ymd');  //订单日期
        $CurrencyType    = '156';  //币种
        $Amount          = number_format($price * 6.8, 2, '.', ''); //订单金额
        $Lang            = 'GB';  //语言
        $Merchanturl     = $huanxun['RETURN_URL']; //支付结果成功同步返回的url
        $FailUrl         = ''; //支付失败返回的url
        $Attach          = $_REQUEST['tel'] . '-' . $dollar; //商户数据包
        $OrderEncodeType = '5'; //订单支付接口加密方式
        $RetEncodeType   = '17';  //交易返回接口加密方式
        $RetType         = '1';  //返回方式
        $ServerUrl       = $huanxun['NOTIFY_URL']; //支付结果成功异步返回的url
        $BillEXP         = '';  //订单有效期
        $GoodsName       = $huanxun['GOODNAME'];  //商品名称
        $IsCredit        = '0';  //直连选项
        $BankCode        = ''; //银行号
        $ProductType     = $IsCredit;  //产品类型
        $xml             = '<Ips><GateWayReq>';
        $body            = "<body><MerBillNo>$MerBillNo</MerBillNo><Amount>$Amount</Amount><Date>$Date</Date><CurrencyType>$CurrencyType</CurrencyType><GatewayType>$GatewayType</GatewayType><Lang>$Lang</Lang><Merchanturl>$Merchanturl</Merchanturl><FailUrl>$FailUrl</FailUrl><Attach>$Attach</Attach><OrderEncodeType>$OrderEncodeType</OrderEncodeType><RetEncodeType>$RetEncodeType</RetEncodeType><RetType>$RetType</RetType><ServerUrl>$ServerUrl</ServerUrl><BillEXP>$BillEXP</BillEXP><GoodsName>$GoodsName</GoodsName><IsCredit>$IsCredit</IsCredit><BankCode>$BankCode</BankCode><ProductType>$ProductType</ProductType></body>";
        $Signature       = md5($body . $MerCode . $Mer_key);
        $head            = "<head><Version>$Version</Version><MerCode>$MerCode</MerCode><MerName>$MerName</MerName><Account>$Account</Account><MsgId>$MsgId</MsgId><ReqDate>$ReqDate</ReqDate><Signature>$Signature</Signature></head>";
        $xml .= $head . $body;
        $xml .= '</GateWayReq></Ips>';
        $form_url = $huanxun['POST_URL'];

        return view('application.Pay', [
            'title'    => '环迅支付',
            'xml'      => $xml,
            'form_url' => $form_url,
        ]);
    }

    public function Notify()
    {
        header('Content-type:text/html; charset=utf-8');
        $huanxun       = Config::get('huanxun');
        $MerCode       = $huanxun['PAY_MID'];
        $Account       = $huanxun['ACCOUNT'];
        $Mer_key       = $huanxun['PAY_KEY'];
        $paymentResult = $_REQUEST['paymentResult'];
        if ('' != $paymentResult) {
            $xml           = simplexml_load_string($paymentResult);
            $xmlHead       = $xml->GateWayRsp->head;
            $xmlBody       = $xml->GateWayRsp->body;
            $RspCode       = $xmlHead->RspCode;
            $RspMsg        = $xmlHead->RspMsg;
            $Signature     = $xmlHead->Signature;
            $MerBillNo     = $xmlBody->MerBillNo;
            $CurrencyType  = $xmlBody->CurrencyType;
            $Amount        = $xmlBody->Amount;
            $Date          = $xmlBody->Date;
            $Attach        = $xmlBody->Attach;
            $Status        = $xmlBody->Status;
            $Msg           = $xmlBody->Msg;
            $Attach        = $xmlBody->Attach;
            $IpsBillNo     = $xmlBody->IpsBillNo;
            $IpsTradeNo    = $xmlBody->IpsTradeNo;
            $RetEncodeType = $xmlBody->RetEncodeType;
            $BankBillNo    = $xmlBody->BankBillNo;
            $ResultType    = $xmlBody->ResultType;
            $IpsBillTime   = $xmlBody->IpsBillTime;
            if ('000000' == $RspCode) {
                if ('Y' == $Status) {
                    $body         = '<body>' . '<MerBillNo>' . $MerBillNo . '</MerBillNo>' . '<CurrencyType>' . $CurrencyType . '</CurrencyType>' . '<Amount>' . $Amount . '</Amount>' . '<Date>' . $Date . '</Date>' . '<Status>' . $Status . '</Status>' . '<Msg><![CDATA[' . $Msg . ']]></Msg>' . '<Attach><![CDATA[' . $Attach . ']]></Attach>' . '<IpsBillNo>' . $IpsBillNo . '</IpsBillNo>' . '<IpsTradeNo>' . $IpsTradeNo . '</IpsTradeNo>' . '<RetEncodeType>' . $RetEncodeType . '</RetEncodeType>' . '<BankBillNo>' . $BankBillNo . '</BankBillNo>' . '<ResultType>' . $ResultType . '</ResultType>' . '<IpsBillTime>' . $IpsBillTime . '</IpsBillTime>' . '</body>';
                    $SignatureNew = md5(($body . $MerCode . $Mer_key));
                    if (strtolower($Signature) == strtolower($SignatureNew)) {
                        $body = explode('-', $Attach);
                        if (PayRequest::where('id_user', $body[0])->where('ips_order', $IpsBillNo)->first()) {
                            header('Location: /account');
                        } else {
                            if ($info = User::where('id_wechat', $body[0])->first()) {
                                //获取系统配置
                                $system   = System::findOrFail(1);
                                $poundage = $body[1] * ($system->interest_rate / 100); //手续费
                                $total    = ($info->body_balance + ($body[1] / 6.8)) - $poundage;
                                if (User::where('id_wechat', $body[0])->update(['body_balance' => $total])) {
                                    PayRequest::create([
                                        'id_user'              => $body[0],
                                        'body_stake'           => $body[1],
                                        'body_gateway'         => '环迅支付',
                                        'poundage'             => $poundage,
                                        'body_transfer_number' => $MerBillNo,
                                        'ips_order'            => $IpsBillNo,
                                        'processed_at'         => date('Y-m-d H:i:s', time()),
                                    ]);
                                    echo 'success';
                                } else {
                                    echo 'fail';
                                }
                            } else {
                                echo 'fail';
                            }
                        }
                    } else {
                        echo 'fail';
                    }
                } else {
                    echo 'error3';
                }
            } else {
                echo 'error2';
            }
        } else {
            echo 'error1';
        }
    }

    /**
     * 订单号生成.
     *
     * @return string
     */
    public function get_order_sn()
    {
        list($usec, $sec) = explode(' ', microtime());
        $mt               = substr($usec, 2, 6);
        $randNum          = mt_rand(100, 999);
        $orderSn          = chr(date('Y') - 1951) . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5)
            . $mt . sprintf('%03d', $randNum);

        return $orderSn;
    }

    public function rechargeRecord()
    {
        //$list = PayRequest::where('id_user', $this->tel)->orderBy('created_at', 'desc')->paginate(10);
        $list = PayRequest::where('id_user', $this->tel)->where('processed_at', '<>', '0000-00-00 00:00:00')->orderBy('created_at', 'desc')->paginate(10);

        return view('application.rechargeRecord', [
            'title' => '充值纪录',
            'list'  => $list,
        ]);
    }

    //判断是否是手机
    public function is_mobile()
    {
        /* $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_mac = (strpos($agent, 'mac os')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        $is_android = (strpos($agent, 'android')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;
        if ($is_pc) {
          return false;
        }
        if ($is_mac) {
          return true;
        }
        if ($is_iphone) {
          return true;
        }
        if ($is_android) {
          return true;
        }
        if ($is_ipad) {
          return true;
        } */

        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset($_SERVER['HTTP_VIA'])) {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = [
                'nokia',
                'sony',
                'ericsson',
                'mot',
                'samsung',
                'htc',
                'sgh',
                'lg',
                'sharp',
                'sie-',
                'philips',
                'panasonic',
                'alcatel',
                'lenovo',
                'iphone',
                'ipod',
                'blackberry',
                'meizu',
                'android',
                'netfront',
                'symbian',
                'ucweb',
                'windowsce',
                'palm',
                'operamini',
                'operamobi',
                'openwave',
                'nexusone',
                'cldc',
                'midp',
                'wap',
                'mobile',
            ];
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match('/(' . implode('|', $clientkeywords) . ')/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((false !== strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml')) && (false === strpos($_SERVER['HTTP_ACCEPT'], 'text/html') || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }

        return false;
    }
}
